<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2016/1/14
 * Time: 11:06
 */

class Involved_NoticeModel extends BaseModel{

    protected $table = 'notice';

    #PC
    /**
     * 消息列表
     * @author alice
     * @date 2016-1-18
     * @return mixed
     */
    public function ShowNoticeList()
    {
        $info = DB::table($this->table)->paginate(10);
        foreach($info as $k => $v) {
            $v->created = date('Y-m-d');
        }
        return $info;
    }

    /**
     * 消息详情
     * @param $id
     * @author alice
     * @date 2016-1-18
     * @return mixed
     */
    public function ShowNoticeInfo($id)
    {
        return DB::table($this->table)->where('notice_id',$id)->first();
    }


    /**
     * 消息中心
     * @param $is_read
     * @param int $page
     * @param $user_id
     * @return mixed
     */
    public function GetNoticeList($is_read, $page = 1, $user_id)
    {
        $offset = ( $page -1 ) * HostEnum::SYSTEM_NOTICE_LIST_LENGTH;
        $oQuery = DB::table($this->table)
                        ->select('notice_id','type','is_read','title','created')
                        ->whereRaw('user_id = 0 OR user_id = '.$user_id);
        $notice_list = $oQuery->orderBy('created', 'desc')
                         ->skip($offset)
                         ->limit(HostEnum::SYSTEM_NOTICE_LIST_LENGTH)
                         ->get();
        $data = [];
        foreach ($notice_list as $k => $notice) {
            if($is_read != HostEnum::SYSTEM_NOTICE_TYPE_ALL && $notice->is_read != $is_read){
                continue;
            }
            $row    =   [
                'notice_id' =>  $notice->notice_id,
                'type'      =>  $notice->type,
                'title'     =>  $notice->title,
                'notice_url'=>  HostEnum::HOST_NAME.'/Api/notice_detail?notice_id='. $notice->notice_id,
                'created'   =>  date('Y-m-d H:i:s',$notice->created)
            ];
            $data[] = $row;
        }

        return $data;
    }

    /**
     * 获取消息内容
     * @param $notice_id
     * @param $user_id
     * @return mixed
     * @throws Exception
     */
    public function GetNoticeDetail($notice_id, $user_id)
    {
        try{
            $notice = DB::table($this->table)
                ->select('notice_id','type','content','title','created')
                ->whereRaw('user_id = 0 OR user_id = '.$user_id.' AND notice_id='.$notice_id)
                ->first();
        }catch (Exception $e){
            throw new Exception("数据库操作失败!", ErrorCodeEnum::STATUS_ERROR);
        }

        if(!$notice)
            throw new Exception("消息ID不存在!", ErrorCodeEnum::STATUS_ERROR);
        $notice->created    =   date('Y-m-d H:i:s',$notice->created);

        return $notice;
    }

    /**
     * 删除消息
     * @param $notice_id
     * @param $user_id
     * @return mixed
     * @throws Exception
     */
    public function DeleteNotice($notice_id, $user_id)
    {
        $check = DB::table($this->table)->where(['user_id' => 0, 'notice_id' => $notice_id])->count();
        if($check)
            throw new Exception("公告消息无法删除!", ErrorCodeEnum::STATUS_ERROR);

        return   DB::table($this->table)->where(['user_id' => $user_id, 'notice_id' => $notice_id])->delete();
    }
}